package org.mlz.game.model

import com.sun.javafx.scene.traversal.Direction
import org.itheima.kotlin.game.core.Painter
import org.mlz.game.Config
import org.mlz.game.business.*
import java.util.*

/**
 *<p>
 *敌方坦克(避开阻挡物)
 * 敌方坦克是可以移动的（自己动起来）
 *  敌方坦克可以组色色移动
 * 敌方坦克可以自动射击
 * 敌方坦克可以被打
 * 敌方坦克是可以销毁
 *</p>
 * CreateBy  Malingzhao
 * @date  2020/9/21
 */
class Enemy(override var x: Int, override var y: Int) : Movable, AutoMovable, Blockable, AutoShot, Sufferable,
    Destroyable {

    /**
     * 生命值
     */
    override var blood: Int = 2


    override var currentDirection: Direction = Direction.DOWN
    override val speed: Int = 8
    override val width: Int = Config.block
    override val height: Int = Config.block

    //坦克不可以走的方向
    private var badDirection: Direction? = null

    private var lastShotTime = 0L
    private var shotFrequency = 800

    private var lastMoveTime = 0L
    private var moveFrequency = 200


    override fun draw() {

        val imgPath: String = when (currentDirection) {
            Direction.UP -> "img/enemy_1_d.gif"
            Direction.DOWN -> "img/enemy_1_d.gif"
            Direction.LEFT -> "img/enemy_1_l.gif"
            Direction.RIGHT -> "img/enemy_1_r.gif"
            Direction.NEXT -> TODO()
            Direction.NEXT_IN_LINE -> TODO()
            Direction.PREVIOUS -> TODO()
        }
        Painter.drawImage(imgPath, x, y)
    }

    override fun notifyCollisionBlock(direction: Direction?, block: Blockable?) {
        badDirection = direction
    }

    override fun autoMove() {

        val current = System.currentTimeMillis()
        if (current - lastMoveTime < moveFrequency) return
        lastMoveTime = current
        if (currentDirection == badDirection) {
            //要往错误的方向走是不允许的
            //改变自己方向
            currentDirection = rdmDirection(badDirection)
            return
        }

        //将要碰撞的时候做判断
        when (currentDirection) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed

        }
        //越界判断
        if (x < 0) x = 0;
        if (x > Config.gameWidth - width) x = Config.gameWidth - width
        if (y < 0) y = 0;
        if (y > Config.gameHeight - height) y = Config.gameHeight - height;
    }

    private fun rdmDirection(bad: Direction?): Direction {
        val i = Random().nextInt(4)
        val direction = when (i) {
            0 -> Direction.UP
            1 -> Direction.DOWN
            2 -> Direction.LEFT
            3 -> Direction.RIGHT
            else -> Direction.UP
        }

        //不能要错误的方向
        if (direction == badDirection) {
            return rdmDirection(bad)
        }
        return direction
    }

    /**
     * 自动射击的功能
     */
    override fun autoShot(): View? {

        val current = System.currentTimeMillis()
        if (current - lastShotTime < shotFrequency) return null
        lastShotTime = current
        return Bullet(this, currentDirection, { bulletWidth, bulletHeight ->
            val tankX = x
            val tankY = y
            val tankWidth = width
            val tankHeight = height
            //计算子弹的真实的坐标
            //如果坦克是向上的
            //bulletX = tankX + (tankWidth - bulletWidth)/2
            //bulletY = tankY - bulletHeight /2

            var bulletX = 0 //x 默认值
            var bulletY = 0 //y 默认值
            var bulletWidth = 16 //不是写死的
            val bulletHeight = 32
            when (currentDirection) {
                Direction.UP -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY - bulletHeight / 2
                }
                Direction.DOWN -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY + tankHeight - bulletHeight / 2
                }
                Direction.LEFT -> {
                    bulletX = tankX - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2
                }
                Direction.RIGHT -> {
                    bulletX = tankX + tankWidth - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2
                }
            }
            Pair(bulletX, bulletY)
        })
    }

    override fun notifySuffer(attackable: Attackable): Array<View>? {
        //子弹的拥有者
        if (attackable.owner is Enemy) {
            //挨打 不掉血 不给反应
            return null
        }

        blood -= attackable.attackPower
        return arrayOf(Blast(x, y))
    }

    /**
     * 判断是否销毁了
     */
    override fun isDestroyed(): Boolean = blood <= 0


}
